package hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import hospital.cmnclient.DictFeignClient;
import hospital.model.hosp.BookingRule;
import hospital.model.hosp.Hospital;
import hospital.repository.HospitalRepository;
import hospital.service.HospitalService;
import hospital.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository repository;

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * @Author yang
     * @Description //TODO 上传医院的接口 或者更新接口
     * @Param [parameterMap]
     * @return
     **/
    @Override
    public void saveOrUpdate(Map<String, Object> parameterMap) {
        //先通过JSONObject 转化成字符串 -> Hospital对象
        String str = JSONObject.toJSONString(parameterMap);
        Hospital hospital = JSONObject.parseObject(str, Hospital.class);

        //判断该对象是否存在
        Hospital hospitalExit=repository.getHospitalByHoscode(hospital.getHoscode());
        //存在则更新
        if(hospitalExit!=null){
            hospital.setId(hospitalExit.getId());
            hospital.setStatus(hospitalExit.getStatus());
            hospital.setCreateTime(hospitalExit.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            repository.save(hospital);
        }else{
            //不存在则添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            repository.save(hospital);
        }
    }

    /**
     * @Author yang
     * @Description //TODO 通过hoscode查询医院数据
     * @Param [requestHoscode]
     * @return
     **/
    @Override
    public Hospital getHospital(String requestHoscode) {
        return repository.getHospitalByHoscode(requestHoscode);
    }

    /**
     * @Author yang
     * @Description //TODO 得到医院的列表
     * @Param [page, limit, vo]
     * @return
     **/
    @Override
    public Page<Hospital> getHospitalList(Integer page, Integer limit, HospitalQueryVo vo) {
        //设置分页规则，第一页为0
        Pageable pageable= PageRequest.of(page-1,limit);

        //将HospitalQueryVo vo -> 转化为 hospital 类
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(vo,hospital);

        //设置模糊查询规则,和忽略大小写规则
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example=Example.of(hospital,matcher);

        //得到医院的列表
        Page<Hospital> pages = repository.findAll(example, pageable);

        //为每个医院设置医院等级 和所在的省市区
        /*for (Hospital item:all){
            this.setHospitalTypeAndProvince(item);
        }(/

         */
        //获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item ->{
            this.setHospitalTypeAndProvince(item);
        });
        return pages;
    }

    /**
     * @Author yang
     * @Description //TODO 更新医院的status,上线或下线
     * @Param [id, status]
     * @return
     **/
    @Override
    public void updateHospitalStatus(String id, Integer status) {
        //根据id，查询当前hospital，修改hospital的status
        Hospital hospital = repository.findById(id).get();
        hospital.setStatus(status);
        repository.save(hospital);
    }

    /**
     * @Author yang
     * @Description //TODO 医院详细信息
     * @Param [id]
     * @return
     **/
    @Override
    public Map<String, Object> showHospitalDetail(String id) {
        Map<String, Object> map=new HashMap<>();

        //根据id查询对应的hospital信息
        Hospital hospital = repository.findById(id).get();
        //包含医院的等级信息
        this.setHospitalTypeAndProvince(hospital);

        map.put("hospital",hospital);
        //把医院的预约规则，保存到map中
        BookingRule bookingRule = hospital.getBookingRule();
        map.put("bookingRule",bookingRule);
        hospital.setBookingRule(null);
        return map;
    }

    /**
     * @Author yang
     * @Description //TODO 据医院的名称，进行模糊查询
     * @Param [hosname]
     * @return
     **/
    @Override
    public List<Hospital> findByHosName(String hosname) {
        List<Hospital> list=repository.findHospitalByHosnameLike(hosname);
        return list;
    }

    /**
     * @Author yang
     * @Description //TODO 根据医院编号获取医院预约挂号的详情
     * @Param [hoscode]
     * @return
     **/
    @Override
    public Map<String, Object> findHospitalDetailByHoscode(String hoscode) {
        Hospital hospital = this.getHospital(hoscode);
        //显示
        Map<String, Object> map = this.showHospitalDetail(hospital.getId());
        return map;
    }

    /**
     * @Author yang
     * @Description //TODO 为每个医院设置医院等级 和所在的省市区
     * @Param [hospital]
     * @return
     **/
    private void setHospitalTypeAndProvince(Hospital hospital){
        String dictName = dictFeignClient.getDictName("Hostype", hospital.getHostype());//医院类型
        String provinceCode = dictFeignClient.getDictName(hospital.getProvinceCode());           //省code
        String cityCode = dictFeignClient.getDictName(hospital.getCityCode());                   //市code
        String districtCode = dictFeignClient.getDictName(hospital.getDistrictCode());           //区code

        //设置参数的
        Map<String, Object> param = hospital.getParam();
        param.put("dictString",dictName);
        param.put("fullAddress",new StringBuilder().append(provinceCode).append(cityCode).append(districtCode));
    }
}
